package com.jucai.pppeiyouclient.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.jucai.pppeiyouclient.MainActivity;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.adapter.FriendGroupAdapter;
import com.jucai.pppeiyouclient.base.BaseActivity;
import com.jucai.pppeiyouclient.base.BaseApplication;
import com.jucai.pppeiyouclient.bean.FriendGroupBean;
import com.jucai.pppeiyouclient.comm.CommonRequestNetData;
import com.jucai.pppeiyouclient.utils.ActivityCollector;
import com.jucai.pppeiyouclient.utils.LogUtils;
import com.jucai.pppeiyouclient.weidget.pulltorefresh.PullToRefreshForRecyclerView;
import com.jucai.pppeiyouclient.weidget.pulltorefresh.PullToRefreshLayout;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class FriendListBakActivity extends BaseActivity implements PullToRefreshLayout.OnRefreshListener {

    private static final String TAG = "FriendListActivity : ";

    private PullToRefreshLayout refreshLayout;
    private PullToRefreshForRecyclerView friendsRv;
    private ArrayList<FriendGroupBean> friends = new ArrayList<>();
    private FriendGroupAdapter friendGroupAdapter;
    private Handler mHandler = new Handler();
    private int page = 1;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.common_activity_with_ptr);

        ((TextView) findViewById(R.id.common_title_text)).setText("关注列表");
        friendGroupAdapter = new FriendGroupAdapter(FriendListBakActivity.this, friends);
        refreshLayout = (PullToRefreshLayout) findViewById(R.id.refresh_view);
        friendsRv = (PullToRefreshForRecyclerView) findViewById(R.id.content_view);
        friendsRv.setLayoutManager(new LinearLayoutManager(FriendListBakActivity.this, LinearLayoutManager.VERTICAL, false));
        friendsRv.setItemAnimator(new DefaultItemAnimator());
        friendsRv.setAdapter(friendGroupAdapter);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void loadNetData() {

        HashMap<String, String> param = new HashMap<>();
        param.put("service", "Public_Chat.getFocusList");
        param.put("page", "1");

        CommonRequestNetData.getJsonArrayData(FriendListBakActivity.this, param, true, new CommonRequestNetData.JsonArrayCallBack() {
            @Override
            public void parseJsonArray(JSONArray jsonArray) {
                LogUtils.e(TAG + "jsArr = " + jsonArray.toString());
                try {
                    friends.clear();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        FriendGroupBean friendListBean = new FriendGroupBean();
                        friendListBean.setMember_focus_cate_id(jsonObject.getString("member_focus_cate_id"));
                        friendListBean.setMember_focus_cate_name(jsonObject.getString("member_focus_cate_name"));
                        friendListBean.setMember_focus_cate_list(jsonObject.getString("member_focus_cate_list"));
                        friends.add(friendListBean);
                    }
                    friendGroupAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    protected void setListener() {
        findViewById(R.id.common_back).setOnClickListener(this);
        findViewById(R.id.common_home_back).setOnClickListener(this);
        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back:
                ActivityCollector.removeActivity(this);
                break;

            case R.id.common_home_back:
                startActivity(new Intent(FriendListBakActivity.this, MainActivity.class));
                ActivityCollector.removeActivity(this);
                break;
        }
    }

    @Override
    public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                loadNetData();
                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
            }
        }, 500);
    }

    @Override
    public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {

        page++;

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                HashMap<String, String> param = new HashMap<>();
                param.put("service", "Public_Chat.getFocusList");
                param.put("page", page + "");

                CommonRequestNetData.getJsonArrayData(FriendListBakActivity.this, param, true, new CommonRequestNetData.JsonArrayCallBack() {
                    @Override
                    public void parseJsonArray(JSONArray jsonArray) {
                        LogUtils.e(TAG + "jsArr = " + jsonArray.toString());
                    }
                });
                pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            }
        }, 500);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (BaseApplication.targetMemberId != null)
            BaseApplication.targetMemberId = null;
    }
}
